package com.igoal.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.service.SaveResult;
import com.igoal.service.UserAccountService;
import com.igoal.utils.StringUtils;

@Controller
public class ActivateAccountController{
	private UserAccountService userAccountService;
	
	@Autowired
	public void setUserAccountService(UserAccountService userAccountService) {
		this.userAccountService = userAccountService;
	}

	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	@RequestMapping(value="/activate.html", method=RequestMethod.GET)
	public String activateAccount(@RequestParam("username") String username, @RequestParam("code") String code){
		if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(code)){
			int result = userAccountService.activateAccount(username, code);
			if(result == SaveResult.SUCCESS){
				return "redirect:login.html";
			}else{
				return "redirect:error/activate.html";
			}
		}else{
			return "redirect:error/activate.html";
		}
	}
}
